package org.jeecg.common.util.mall;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext context = null;
    private static Properties prop;
    private static Properties prop2;

    public static <T> T getBean(String beanName) {
        return (T) context.getBean(beanName);
    }

    public static <T> T getBean(Class<T> beanClass) {
        return context.getBean(beanClass);
    }


    public static String getProperty(String property) {
        if (prop == null) {
            prop = new Properties();
            try {
                InputStream in = context.getResource("classpath:properties/common.properties").getInputStream();
                prop.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return prop.getProperty(property).trim();
    }

    public static String getEnvironmentProperty(String property) {
        return context.getEnvironment().getProperty(property);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}